package com.loong.web.gp4j.component.velocity;

import java.io.StringWriter;

import javax.annotation.Resource;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.stereotype.Component;

/**
 * 模板组件
 *
 * @author 张成轩
 */
@Component
public class VelocityComponent {

	@Resource
	private VelocityEngine velocity;

	/**
	 * 获取内容
	 * 
	 * @param vm 模板
	 * @return 内容
	 */
	public String context(String vm) {

		return context(vm, new VelocityContext());
	}

	/**
	 * 获取内容
	 * 
	 * @param vm 模板
	 * @param vc 参数集
	 * @return 内容
	 */
	public String context(String vm, VelocityContext vc) {

		Template t = velocity.getTemplate(vm);
		StringWriter sw = new StringWriter();
		t.merge(vc, sw);
		return sw.toString();
	}
}
